using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace 新工具箱
{
    public partial class 主界面窗口: Form
    {
        public 主界面窗口()
        {
            InitializeComponent();
            this.Load += 主界面窗口_Load;
        }

        private void 主界面窗口_Load(object sender, EventArgs e)
        {
           
        }
        

            private void Button1_click(object sender, EventArgs e)
            {
                this.Text = "离开";
                // 打开新的一元二次方程求解界面
                一元二次方程求解界面 newForm = new 一元二次方程求解界面();
                newForm.Show();
                // 按钮被点击一次就会失效变成灰色 button1.Enabled = false;
                
            }

        

        private void Button2_Click(object sender, EventArgs e)
        {

        }

        private void Button3_Click(object sender, EventArgs e)
        {

        }
    }
}
public partial class 一元二次方程求解界面 : Form
{
    private TextBox txtA;
    private TextBox txtB;
    private TextBox txtC;
    private Button btnSolve;
    private Label lblResult;

    public 一元二次方程求解界面()
    {
      //  InitializeComponent();
        InitializeControls();//初始化窗口
    }

    private void InitializeControls()
    {
        // 创建并添加PictureBox控件用于显示图片
        PictureBox picBox = new PictureBox();
        picBox.Location = new Point(180, 30);
        picBox.Size = new Size(200, 150);
        picBox.SizeMode = PictureBoxSizeMode.StretchImage;

        // 从项目资源中加载图片
        picBox.Image = 新工具箱.Properties.Resources.下载;

        this.Controls.Add(picBox);

        // 创建并添加用于提示的Label控件
        Label lblEquationType = new Label();
        lblEquationType.Text = "一元二次方程写成：ax²＋bx＋c的格式";
        lblEquationType.Location = new Point(50, 10);
        lblEquationType.Size = new Size(300,30);
        this.Controls.Add(lblEquationType);

        // 创建并设置文本框用于输入系数
        txtA = new TextBox();
        txtA.Location = new Point(50, 50);//文本框的点位
        txtA.Size = new Size(100, 20);//文本框的大小，大小会限制文字显示
        txtA.Text = "请输入a的值";
        txtA.GotFocus += TxtA_GotFocus;
        txtA.LostFocus += TxtA_LostFocus;
        this.Controls.Add(txtA);

        txtB = new TextBox();
        txtB.Location = new Point(50, 80);
        txtB.Size = new Size(100, 20);
        txtB.Text = "请输入b的值";
        txtB.GotFocus += TxtB_GotFocus;
        txtB.LostFocus += TxtB_LostFocus;
        this.Controls.Add(txtB);

        txtC = new TextBox();
        txtC.Location = new Point(50, 110);
        txtC.Size = new Size(100, 20);
        txtC.Text = "请输入c的值";
        txtC.GotFocus += TxtC_GotFocus;
        txtC.LostFocus += TxtC_LostFocus;
        this.Controls.Add(txtC);

        // 创建并设置求解按钮
        btnSolve = new Button();
        btnSolve.Text = "求解";
        btnSolve.Location = new Point(50, 140);
        btnSolve.Size = new Size(100, 25);
        btnSolve.Click += JieFangCheng;
        this.Controls.Add(btnSolve);

        // 创建并设置用于显示结果的标签
        lblResult = new Label();
        lblResult.Location = new Point(50, 170);
        lblResult.Size = new Size(200, 20);
        this.Controls.Add(lblResult);
    }
    private void TxtA_GotFocus(object sender, EventArgs e)
    {
        if (txtA.Text == "请输入a的值")
        {
            txtA.Text = "";
        }
    }

    private void TxtA_LostFocus(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtA.Text))
        {
            txtA.Text = "请输入a的值";
        }
    }

    private void TxtB_GotFocus(object sender, EventArgs e)
    {
        if (txtB.Text == "请输入b的值")
        {
            txtB.Text = "";
        }
    }

    private void TxtB_LostFocus(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtB.Text))
        {
            txtB.Text = "请输入b的值";
        }
    }

    private void TxtC_GotFocus(object sender, EventArgs e)
    {
        if (txtC.Text == "请输入c的值")
        {
            txtC.Text = "";
        }
    }

    private void TxtC_LostFocus(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtC.Text))
        {
            txtC.Text = "请输入c的值";
        }
    }
    //一元二次方程的求解↓
    private void JieFangCheng(object sender, EventArgs e)
    {
        if (double.TryParse(txtA.Text, out double a) &&
            double.TryParse(txtB.Text, out double b) &&
            double.TryParse(txtC.Text, out double c))
        {
            double discriminant = b * b - 4 * a * c;

            if (discriminant > 0)
            {
                double root1 = (-b + Math.Sqrt(discriminant)) / (2 * a);
                double root2 = (-b - Math.Sqrt(discriminant)) / (2 * a);
                lblResult.Text = $"方程的两个根为：{root1} 和 {root2}";
            }
            else if (discriminant == 0)
            {
                double root = -b / (2 * a);
                lblResult.Text = $"方程的根为：{root}";
            }
            else
            {
                lblResult.Text = "方程无实数根";
            }
        }
        else
        {
            lblResult.Text = "请输入正确的数字系数";
        }
    }
}

